<?php
declare (strict_types = 1);

namespace app\model\communal;

use app\model\concern\AmisTrait;
use think\Model;
use think\db\Query;
use think\model\relation\BelongsTo;
use think\model\concern\SoftDelete;
use think\model\relation\HasMany;

class CourseModel extends Model
{
    use AmisTrait;

    /**
     * 数据库配置
     *
     * @var string
     */
    protected $connection = 'communal';

    /**
     * 主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 表名
     *
     * @var string
     */
    protected $table = '82000_course';

    /**
     * 自动时间戳
     *
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间字段设置
     *
     * @var string
     */
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    /**
     * 软删除
     */
    use SoftDelete;
    protected $deleteTime = 'delete_at';
    protected $defaultSoftDelete = 0;

    /**
     * 展示字段名
     *
     * @var string[]
     */
    public static $column = [
        'id', 'admin_id', 'course_category_id', 'name', 'image', 'type', 'amount', 'content', 'expired_days', 'state', 'created_at'
    ];

    /**
     * 搜索字段名
     *
     * @var string[]
     */
    public static $searchColumn = [
        'id', 'course_category_id', 'name', 'type', 'state', 'createtime',
        'price_option',
    ];

    /**
     * 新增字段
     *
     * @var string[]
     */
    public static $saveColumn = ['course_category_id', 'name', 'image', 'type', 'amount', 'expired_days', 'content', 'state'];

    /**
     * 更新字段
     *
     * @var string[]
     */
    public static $updateColumn = ['id', 'course_category_id', 'name', 'image', 'type', 'amount', 'expired_days', 'content'];

    /**
     * @var string[]
     */
    public static $itemColumn = ['id', 'name', 'state'];

    /**
     * 排序选项 Options
     */
    public static $typeOptions = [
        ['id' => 1, 'name' => '直播课程'],
        ['id' => 2, 'name' => '点播课程'],
    ];

    /**
     * 排序选项 Options
     */
    public static $orderOptions = [
        ['id' => 1, 'name' => '综合排序'],
        // ['id' => 2, 'name' => '人气从高到低'],
        ['id' => 3, 'name' => '价格从高到低'],
        ['id' => 4, 'name' => '价格从低到高'],
        ['id' => 5, 'name' => '上架时间从新到旧'],
    ];

    /**
     * 排序规则映射
     *
     * @var string[]
     */
    public static $orderRuleMap = [
        1 => ['orderBy' => 'created_at', 'orderDir' => 'desc'],
        // 2 => ['orderBy' => 'amount', 'orderDir' => 'asc'],
        3 => ['orderBy' => 'amount', 'orderDir' => 'desc'],
        4 => ['orderBy' => 'amount', 'orderDir' => 'asc'],
        5 => ['orderBy' => 'created_at', 'orderDir' => 'desc'],
    ];

    /**
     * 测试价格选项 Options
     */
    public static $priceOptions = [
        ['id' => 1, 'name' => '免费'],
        ['id' => 2, 'name' => '付费'],
    ];

    /**
     * ids 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchIdsAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $query->whereIn('id', $value);
        }
    }

    /**
     * name 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchNameAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $query->where('name', 'like', '%' . $value . '%');
        }
    }

    /**
     * type 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchTypeAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $query->where('type', $value);
        }
    }

    /**
     * course_category_id 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchCourseCategoryIdAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $query->where('course_category_id', $value);
        }
    }

    /**
     * price_option 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchPriceOptionAttr(Query $query, $value)
    {
        switch ($value) {
            // 免费
            case 1:
                $query->where('amount', '<=', 0);
                break;
            case 2:
                $query->where('amount', '>', 0);
                break;
        }
    }

    /**
     * @return BelongsTo
     */
    public function belongCategory()
    {
        return $this->belongsTo(CourseCategoryModel::class, 'course_category_id', 'id')
            ->bind([
                'category_name' => 'name',
            ]);
    }

    /**
     * @return HasMany
     */
    public function manyChapters()
    {
        return $this->hasMany(CourseChapterModel::class, 'course_id', 'id');
    }

    /**
     * @return HasMany
     */
    public function manySections()
    {
        return $this->hasMany(CourseSectionModel::class, 'course_id', 'id');
    }
}
